package com.customization.test.demo.util;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.engine.common.util.ServiceUtil;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.file.util.FileDeleteUtil;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;


import com.engine.workflow.biz.freeNode.FreeNodeBiz;
import com.engine.workflow.entity.freeNode.FreeNodeEntity;
import com.engine.workflow.service.HtmlToPdfService;
import com.engine.workflow.service.impl.HtmlToPdfServiceImpl;

import weaver.workflow.exceldesign.HtmlLayoutOperate;

/**
 * @Author
 * @Description
 */
public class WorkflowToDocUtils extends BaseBean {

    private static BaseBean baseBean = new BaseBean();

    public WorkflowToDocUtils() {
    }

    public static void createHtml(RequestInfo requestInfo, String path, String filename) {
        if (requestInfo == null || path == null || path.trim().equals("") || filename == null || filename.trim().equals("")) {
            return;
        }
        try {
            User user = requestInfo.getRequestManager().getUser();
            String requestid = Util.null2String(requestInfo.getRequestid());
            String workflowid = Util.null2String(requestInfo.getWorkflowid());
            String src = requestInfo.getRequestManager().getSrc(); //用于判断是否流程监控干预
            int nodeid = requestInfo.getRequestManager().getNodeid();
            int keepsign = getKeepSign(workflowid);
            String modeid = getModeid(Util.getIntValue(workflowid), Util.getIntValue(requestid), nodeid);

            Map<String, Object> params = new HashMap<>();
            params.put("requestid", "" + requestid); //必传
            params.put("modeid", modeid);
            params.put("onlyHtml", "2");  //0:转pdf ?1:只转成html ?2:转html和pdf ?(不传则默认=0)
            params.put("pageSize", "100");  //底部签字意见最大显示数量 ?(默认=100)
            params.put("path", path);
            params.put("filename", filename);
            params.put("keepsign", "" + keepsign);//1:保留底部签字意见 0：不保留 (不传则默认=1)
            params.put("limitauth", "0"); //不校验权限
            params.put("src", src);
            params.put("onlinehtml", true);//在线HTML
            params.put("isOpenWaterMark", false);

            HtmlToPdfService htmlToPdfService = (HtmlToPdfService) ServiceUtil.getService(HtmlToPdfServiceImpl.class, user);
            Map<String, Object> pathMap = htmlToPdfService.getFormDatas(params);

            FileDeleteUtil fileDeleteUtil = new FileDeleteUtil();
            fileDeleteUtil.deleteFile(path + filename + ".zip");
            fileDeleteUtil.deleteFolder(path + filename + File.separator + "images" + File.separator);
            fileDeleteUtil.deleteFolder(path + filename + File.separator);
        } catch (Exception ex) {
            baseBean.writeLog("createHtml=" + ex);
        }
    }

    /*
     *得到keepsign 是否保留签字意见
     */
    private static int getKeepSign(String workflowid) {
        RecordSet rs = new RecordSet();
        int keepsign = 0;
        rs.executeQuery("select keepsign from workflow_base where id = ?", workflowid);
        if (rs.next()) {
            keepsign = rs.getInt("keepsign");
        }
        return keepsign;
    }

    /*
     *获取模板id
     */
    public static String getModeid(int workflowid, int requestid, int nodeid) {
        RecordSet rs = new RecordSet();
        int modeid = -1;
        FreeNodeEntity nodeEntity = FreeNodeBiz.getCurrentFreeNodeEntity(requestid, nodeid);
        if (nodeEntity != null) {  //如果为自由节点
            nodeid = nodeEntity.getExtendnodeid();
        }

        HtmlLayoutOperate htmlLayoutOperate = new HtmlLayoutOperate();

        rs.executeQuery("select * from workflow_printset where nodeid = ? and type =1 and printenable =1 order by orderid", nodeid);
        if (rs.next()) {
            modeid = Util.getIntValue(rs.getString("modeid"), -1);
        } else {
            rs.executeQuery("select ismode from workflow_flownode  where workflowid=? and nodeid=?", workflowid, nodeid);
            if (rs.next()) {
                String ismode = Util.null2String(rs.getString("ismode"));
                if (ismode.equals("2")) {   //如果不是
                    modeid = htmlLayoutOperate.getActiveHtmlLayout(workflowid, nodeid, 0);   //获取当前显示模板的活动模板
                } else {
                    modeid = -1;
                }
            }
        }

        return modeid + "";
    }
}
